package com.jqb.sturts2lab02.action;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class Struts2FileDownloadAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	
	/**
	 * 其实filename只是下载的时候用来显示名称可以是任意的,
	 * 真正需要下载的文件是通过getResourceAsStream(realfile)来指定的
	 * */
	private String filename;
	private int num;
	
	public InputStream getDownloadFile(){
		if(num==1){
			filename="1.jsp";
			return ServletActionContext.getServletContext().getResourceAsStream("/userupload/"+filename);
		}else if(num==2){
			filename="2.jsp";
			return ServletActionContext.getServletContext().getResourceAsStream("/userupload/"+filename);
		}
		else if(num==3){
			try {
				filename="解决中文名.jsp";
				filename=new String(this.filename.getBytes("gbk"),"ISO8859-1");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			return ServletActionContext.getServletContext().getResourceAsStream("/userupload/"+"解决中文名.jsp");
		}
		return null;
	}
	
	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}

	public String getFilename() {
		return filename;
	}

	public void setFilename(String filename) {
		this.filename = filename;
	}

	public int getNum() {
		return num;
	}

	public void setNum(int num) {
		this.num = num;
	}

}
